﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using XRM.Security.DataAccess.Helpers;
using XRM.Security.DataAccess.Models;
using XRM.Security.WebUI.Models.Common;

namespace XRM.Security.WebUI.Helpers
{
    public static class SelectedListHelper
    {
        public static SelectListItem DefaultAllItem
        {
            get { return new SelectListItem { Text = "Все", Value = String.Empty }; }
        }

        public static SelectListItem EmptyPCItem
        {
            get { return new SelectListItem {Text = "(Выберите серийный номер ПК)",Value = String.Empty}; }
        }

        public static SelectListItem EmptyDepartmentAddressItem
        {
            get { return new SelectListItem { Text = "(Выберите адрес)", Value = String.Empty }; }
        }

        public static SelectListItem EmptyDepartmentItem
        {
            get { return new SelectListItem { Text = "(Выберите подразделение)", Value = String.Empty }; }
        }

        public static List<SelectListItem> ToSelectedList( this IEnumerable<InfoSystem> list )
        {
            return list.OrderBy( s => s.InfoSystemName ).Select( s => new SelectListItem { Text = s.InfoSystemName, Value = s.InfoSystemId.ToString() } ).ToList();
        }

        public static List<SelectListItem> ToSelectedList( this IEnumerable<IdentityCenter> list )
        {
            return list.OrderBy( s => s.IdentityCenterName ).Select( s => new SelectListItem { Text = s.IdentityCenterName, Value = s.IdentityCenterId.ToString() } ).ToList();
        }

        public static List<SelectListItem> ToSelectedList( this IEnumerable<SkziKind> list )
        {
            return list.OrderBy( s => s.ShortName ).Select( s => new SelectListItem { Text = s.ShortName, Value = s.SkziKindId.ToString() } ).ToList();
        }

        public static List<SelectListItem> ToSelectedList( this IEnumerable<WorkKind> list )
        {
            return list.OrderBy( s => s.WorkKindName ).Select( s => new SelectListItem { Text = s.WorkKindName, Value = s.WorkKindId.ToString() } ).ToList();
        }

        public static List<SelectListItem> ToSelectedList( this IEnumerable<Department> list )
        {
            return list.OrderBy( s => s.Name ).Select( s => new SelectListItem { Text = s.Name, Value = s.DepartmentId.ToString() } ).ToList();
        }

        public static List<SelectListItem> ToSelectedList( this IEnumerable<SystemUser> list )
        {
            return list.Select( s => new SelectListItem { Text = s.FullName(), Value = s.Id.ToString() } ).OrderBy( s => s.Text ).ToList();
        }

        public static List<SelectListItem> ToSelectedList( this IEnumerable<PC> list )
        {
            return list.Select( s => new SelectListItem { Text = s.SerialNumber, Value = s.PCId.ToString() } ).OrderBy( s => s.Text ).ToList();
        }

        public static List<SelectListItem> ToSelectedList( this IEnumerable<DepartmentAddress> list )
        {
            return list.Select( s => new SelectListItem { Text = s.Address, Value = s.DepartmentAddressId.ToString() } ).OrderBy( s => s.Text ).ToList();
        }

        public static List<ItemCheckBoxModel> ToCheckBoxItemsList( this IEnumerable<WorkKind> list )
        {
            return list.OrderBy( s => s.WorkKindName ).Select( s => new ItemCheckBoxModel { Label = s.WorkKindName, Code = s.WorkKindId.ToString(), IsChecked = true } ).ToList();
        }

        public static List<SelectListItem> AddFirst( this List<SelectListItem> list, SelectListItem firstItem )
        {
            list.Insert(0, firstItem);
            return list;
        }

        public static object ToJsonObject( this List<SelectListItem> list )
        {
            return list.Select( s => new { s.Text, s.Value } );
        }
    }
}